package com.xuanzheng.company.service.impl;

import com.xuanzheng.company.dao.AttendanceSettingDao;
import com.xuanzheng.company.dao.impl.AttendanceSettingDaoImpl;
import com.xuanzheng.company.entity.AttendanceSetting;
import com.xuanzheng.company.service.AttendanceSettingService;

import java.sql.Time;
import java.time.LocalTime;

public class AttendanceSettingServiceImpl implements AttendanceSettingService {
    private AttendanceSettingDao attendanceSettingDao;
    
    public AttendanceSettingServiceImpl() {
        this.attendanceSettingDao = new AttendanceSettingDaoImpl();
    }
    
    @Override
    public AttendanceSetting getByCompanyId(String companyId) {
        return attendanceSettingDao.getByCompanyId(companyId);
    }
    
    @Override
    public boolean saveOrUpdate(AttendanceSetting setting) {
        return attendanceSettingDao.saveOrUpdate(setting) > 0;
    }
    
    @Override
    public boolean delete(String id) {
        return attendanceSettingDao.delete(id) > 0;
    }
    
    @Override
    public boolean isInCheckInTimeRange(String companyId) {
        AttendanceSetting setting = getByCompanyId(companyId);
        if (setting == null) {
            System.out.println("未找到公司[" + companyId + "]的打卡设置");
            return false;
        }
        
        // 获取当前时间
        LocalTime now = LocalTime.now();
        
        // 获取上班打卡时间范围
        LocalTime startTime = setting.getCheckInStartTime().toLocalTime();
        LocalTime endTime = setting.getCheckInEndTime().toLocalTime();
        
        System.out.println("上班打卡时间范围检查 - 公司ID: " + companyId);
        System.out.println("当前时间: " + now);
        System.out.println("上班打卡开始时间: " + startTime);
        System.out.println("上班打卡结束时间: " + endTime);
        
        // 检查当前时间是否在上班打卡时间范围内
        boolean inRange = !now.isBefore(startTime) && !now.isAfter(endTime);
        System.out.println("是否在上班打卡时间范围内: " + inRange);
        
        return inRange;
    }
    
    @Override
    public boolean isInCheckOutTimeRange(String companyId) {
        AttendanceSetting setting = getByCompanyId(companyId);
        if (setting == null) {
            System.out.println("未找到公司[" + companyId + "]的打卡设置");
            return false;
        }
        
        // 获取当前时间
        LocalTime now = LocalTime.now();
        
        // 获取下班打卡时间范围
        LocalTime startTime = setting.getCheckOutStartTime().toLocalTime();
        LocalTime endTime = setting.getCheckOutEndTime().toLocalTime();
        
        System.out.println("下班打卡时间范围检查 - 公司ID: " + companyId);
        System.out.println("当前时间: " + now);
        System.out.println("下班打卡开始时间: " + startTime);
        System.out.println("下班打卡结束时间: " + endTime);
        
        // 检查当前时间是否在下班打卡时间范围内
        boolean inRange = !now.isBefore(startTime) && !now.isAfter(endTime);
        System.out.println("是否在下班打卡时间范围内: " + inRange);
        
        return inRange;
    }
}